﻿
package com.victim.snake.room
{
	import com.victim.snake.user.ProcessMsg;
	
	import flash.utils.ByteArray;
	
	import com.victim.snake.user.User;	
	import com.victim.snake.player.Player;
	import com.victim.snake.player.PlayerBo;
	import com.victim.snake.player.PlayerEvent;

	//房间业务类
	public class RoomBo{

		public function RoomBo():void{
			
		}
		
		//向服务器发送当前玩家准备或不准备消息消息 0：不准备  1：准备
		static public function sendReadyOrUnReadyMsg():void{			
			PlayerBo.sendReadyOrUnReadyMsg(User.getPlayer());	//调用玩家操作类的发送准备或非准备消息函数
		}
		
		
		//设置房间里指定ID的玩家的准备状态
		static public function setPlayerReadyOrUnReadyById(strPlayerId:String,readyOrUnReady:Boolean):void{
			PlayerBo.setReadyOrUnReady(RoomBo.getPlayerById(int(strPlayerId)),readyOrUnReady);
		}
		
		//设置房间里指定ID的玩家的snakeStyle
		static public function setSnakeStyleById(strPlayerId:String,snakeStyleName:String):void{
			PlayerBo.setSnakeStyle(RoomBo.getPlayerById(int(strPlayerId)),snakeStyleName);
		}
		
		//房间内玩家是否全部准备
		static public function isAllReady():Boolean{
			var players:Array=User.getRoom().getPlayers();
			var player:Player=null;
			var isAllReady:Boolean=true;

			for(var i:int=0;i<8;i++){
				if(players[i]!=null && players[i]!=User.getRoom().getMaster()){	//如果玩家不为空并且非房主
					player=Player(players[i]);
					if(!player.getIsReady()){	//如果有玩家末准备
						isAllReady=false;
					}
				}
			}
			return isAllReady;
		}
		
		
		
		//转换String类型的准备或非准备消息为boolean类型
		static public function tranReadyOrUnReadyStrMsg(readyOrUnReadyStrMsg:int):Boolean{
			if(int(readyOrUnReadyStrMsg)==0){
				return false;
			}else{
				return true;
			}
		}
		
		//返回房间里指定ID的玩家
		static public function getPlayerById(playerId:int):Player{
			return User.getRoom().getPlayerById(playerId);
		}
		
		//添加用户到指定的房间位置和队伍中并反回用户在房间中的位置.如果返回-1,则添加失败  该方法在添加的同时会在player上添加PlayerEvent.Ready及PlayerEvent.UnReady监听
		static public function addPlayer(player:Player,groupId:int,site:int=-1):int{
			return User.getRoom().addPlayer(player,groupId,site);
		}

	}

}